package com.jcf.springboot.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.sys.Menu;
import com.jcf.springboot.service.MenuService;
import com.jcf.springboot.service.impl.MenuServiceImpl;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-03-19
 */
@RestController
@CrossOrigin
@RequestMapping("/menu")
public class MenuController {

  @Resource
  private MenuService menuService;

  @RequestMapping("getList")
  public R getList() {
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.orderByAsc("`order_num`");
    List<Menu> all = menuService.list(queryWrapper);
    List<Menu> menuList = new ArrayList<>();
    for (Menu menu : all)
    {
      if (menu.getParentId().equals("0"))
      {
        menuList.add(menu);
      }
    }
    for (Menu menu : menuList)
    {
      menu.setChildren(new MenuServiceImpl().getChild(menu.getId(), all));
    }
    if (menuList.size() == 0)
    {
      return R.error().message("获取菜单列表失败");
    }
    else
    {
      return R.ok().data(menuList).message("获取菜单列表成功");
    }

  }

  @GetMapping("getMenuByRole")
  public R getMenuByRole(String rid) {
    List<String> list = menuService.getMenuByRole(rid);
    if (null == list)
    {
      return R.error().message("获取失败");
    }
    else
    {
      return R.ok().data(list).message("获取成功");
    }
  }

  @PutMapping("updatePermission")
  public R updatePermissionByRoles(@RequestParam String rid, @RequestParam List<String>menuIds) {
    System.out.println(".....................");
    Boolean res = menuService.updatePermissionByRoles(rid, menuIds);
    if (res)
    {
      return R.ok().message("更新成功");

    }

    else return R.error().message("更新失败");

  }

  @PostMapping("addMenu")
  public R addMenu(@RequestBody Menu menu){
    //管理员默认赋予全部权限
    boolean save = menuService.save(menu);
    if(save){
      return R.ok().message("添加成功");

    }
    else return R.error().message("添加失败");

  }

  @PostMapping("editMenu")
  public R editMenu(@RequestBody Menu menu){
    //管理员默认赋予全部权限
    boolean save = menuService.updateById(menu);
    if(save){
      return R.ok().message("编辑成功");

    }
    else return R.error().message("编辑失败");

  }

  @PostMapping("deleteMenu")
  public R deleteMenu(@RequestBody Menu menu){

    menuService.deleteMenu(menu);
    if(menu.getResult1() <= 0){
      if(menu.getResult2() > 0){
        return R.ok().message("删除成功");
      }
      else{
        return R.error().message("删除失败");
      }
    }
    else{
      return R.error().message("存在关联的角色数据，无法删除");
    }
  }
}

